/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sorm;

import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author sapan
 */
public class ConnectionPool {

    private static ArrayList connections = new ArrayList();
    private static int leasedConnections = 0;

    private ConnectionPool() {

    }

    public static MySqlConnection leaseConnection() {
        MySqlConnection con = null;
        if ((connections.size() > 0)) {
            con = (MySqlConnection) connections.remove(connections.size()-1);
            

        } else if (connections.size() == 0 && leasedConnections == 0) {
            for (int i = 0; i < 10; i++) {
                connections.add(new MySqlConnection());
                leasedConnections++;
            }
            con = (MySqlConnection) connections.get(0);
        } else if (connections.size() == 0 && leasedConnections != 0) {

            con = new MySqlConnection();

        }
        return con;
    }

    public static void releaseConnection(MySqlConnection conn) {
        if(conn!=null)
            conn.clear();
        if (connections.size() < 10) {
            connections.add(conn);
        } else {
            try {
                if(conn!=null)
                conn.close();
            } catch (SQLException ex) {

            }
        }
    }
}
